package cc.mrbird.febs.accounting.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.accounting.entity.CompanyProject;
import cc.mrbird.febs.accounting.service.ICompanyProjectService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author baoyinlei
 * @date 2021-09-28 23:26:58
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class CompanyProjectController extends BaseController {

    private final ICompanyProjectService companyProjectService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "companyProject")
    public String companyProjectIndex(){
        return FebsUtil.view("companyProject/companyProject");
    }

    @GetMapping("companyProject")
    @ResponseBody
    @RequiresPermissions("companyProject:list")
    public FebsResponse getAllCompanyProjects(CompanyProject companyProject) {
        return new FebsResponse().success().data(companyProjectService.findCompanyProjects(companyProject));
    }//通过数据库查询到相关的记录

    @GetMapping("companyProject/list")
    @ResponseBody
    @RequiresPermissions("companyProject:list")
    public FebsResponse companyProjectList(QueryRequest request, CompanyProject companyProject) {
        Map<String, Object> dataTable = getDataTable(this.companyProjectService.findCompanyProjects(request, companyProject));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增CompanyProject", exceptionMessage = "新增CompanyProject失败")
    @PostMapping("companyProject")
    @ResponseBody
    @RequiresPermissions("companyProject:add")
    public FebsResponse addCompanyProject(@Valid CompanyProject companyProject) {
        this.companyProjectService.createCompanyProject(companyProject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除CompanyProject", exceptionMessage = "删除CompanyProject失败")
    @GetMapping("companyProject/delete")
    @ResponseBody
    @RequiresPermissions("companyProject:delete")
    public FebsResponse deleteCompanyProject(CompanyProject companyProject) {
        this.companyProjectService.deleteCompanyProject(companyProject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改CompanyProject", exceptionMessage = "修改CompanyProject失败")
    @PostMapping("companyProject/update")
    @ResponseBody
    @RequiresPermissions("companyProject:update")
    public FebsResponse updateCompanyProject(CompanyProject companyProject) {
        this.companyProjectService.updateCompanyProject(companyProject);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改CompanyProject", exceptionMessage = "导出Excel失败")
    @PostMapping("companyProject/excel")
    @ResponseBody
    @RequiresPermissions("companyProject:export")
    public void export(QueryRequest queryRequest, CompanyProject companyProject, HttpServletResponse response) {
        List<CompanyProject> companyProjects = this.companyProjectService.findCompanyProjects(queryRequest, companyProject).getRecords();
        ExcelKit.$Export(CompanyProject.class, response).downXlsx(companyProjects, false);
    }
}
